import Vue from 'vue'
import Router from 'vue-router'
// import Login from './../components/Login.vue' 不采用这种 使用路由懒加载
const Login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './../components/Login.vue')
const Home = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './../components/Home.vue')
const Welcome = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './../components/Welcome.vue')
const Users = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ './../components/user/Users.vue')
const Rights = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ './../components/power/Rights.vue')
const Roles = () =>
    import ( /* webpackChunkName: "Users_Rights_Roles" */ './../components/power/Roles.vue')
const Cate = () =>
    import ( /* webpackChunkName: "Params_Cate" */ './../components/goods/Cate.vue')
const Params = () =>
    import ( /* webpackChunkName: "Params_Cate" */ './../components/goods/Params.vue')
const Goodslist = () =>
    import ( /* webpackChunkName: "Goodslist_Add" */ './../components/goods/List.vue')
const Add = () =>
    import ( /* webpackChunkName: "Goodslist_Add" */ './../components/goods/Add.vue')
const Order = () =>
    import ( /* webpackChunkName: "Order_Report" */ './../components/order/Order.vue')
const Report = () =>
    import ( /* webpackChunkName: "Order_Report" */ './../components/report/Report.vue')

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login }, // 用户访问login页面时，使用Login组件渲染
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/rights', component: Rights },
                { path: '/roles', component: Roles },
                { path: '/categories', component: Cate },
                { path: '/params', component: Params },
                { path: '/goods', component: Goodslist },
                { path: '/goods/add', component: Add },
                { path: '/orders', component: Order },
                { path: '/reports', component: Report },

            ]
        },
        {
            path: "page404",
            meta: {
                name: "404",
                icon: "404"
            },
            component: Home,
            children: [{
                    path: '/404',
                    meta: { name: "404" },
                    component: () =>
                        import ('./../components/404.vue')
                },

            ]
        },
        { path: '*', redirect: '/404', hidden: true },

    ]
})

// 挂载路由导航守卫

router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 表示函数 表示放行
    // next() 放行 next('/login') 强制跳转
    if (to.path === '/login') return next() //登录页放行

    // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')

    next()


})


export default router